#include "JNIManager.h"

#ifdef Q_OS_ANDROID

static void printInfo(JNIEnv *env, jobject thiz, jstring info){
    QString result;
    const char *nativeString = env->GetStringUTFChars(info, 0);
    result = nativeString;
    qDebug() << "打印消息：" << result;
}
static const JNINativeMethod gMethods[] = {
    {"printInfo", "(Ljava/lang/String;)V", (void*)printInfo}
};



JNIManager::JNIManager()
{
    // Java方法注册
    QtAndroid::runOnAndroidThreadSync([=](){
        QAndroidJniEnvironment Environment;
        QAndroidJniObject javaClass("org/qtproject/example/ExtendsQtNative");
        jclass j_class = Environment->GetObjectClass(javaClass.object<jobject>());
        if (j_class == nullptr)
        {
            qDebug() << __FUNCTION__ <<"erro clazz";
            return ;
        }
        jint mj = Environment->RegisterNatives(j_class, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
        if (mj != JNI_OK)
        {
            qDebug() << __FUNCTION__ << "register native method failed!";
            return;
        }else{
            qDebug() << __FUNCTION__ << "RegisterNatives success!";
        }
    });

}

JNIManager& JNIManager::I(){
    static JNIManager instance;
    return instance;
}

QStringList JNIManager::getFileList(QString path){

    qDebug("JNI调用");

    qDebug() << "加载路径：" << path;

    QAndroidJniEnvironment env;
    jclass cls = env.findClass("org/qtproject/example/ExtendsQtWithJava");
    if(cls == NULL){
        qDebug("查找类失败");
    }
    jmethodID mid = env->GetStaticMethodID(cls, "getFileList", "(Ljava/lang/String;)Ljava/lang/String;");
    if(mid == NULL){
        qDebug("查找方法失败");
    }

    QAndroidJniObject pathObject = QAndroidJniObject::fromString(path);
    QAndroidJniObject stringJni = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/ExtendsQtWithJava", "getFileList", "(Ljava/lang/String;)Ljava/lang/String;", pathObject.object<jstring>());

    QString result = stringJni.toString();
    if(result == "PERMISSION_DENIED"){
        QStringList res;
        return res;
    }

    QStringList res = result.split(",");
    return res;

}       // 出错时返回0，列表为空时返回一个空的res


QStringList JNIManager::getFileList_SMB(QString ip, QString usr, QString pwd, QString path){

    QStringList res;

    QAndroidJniObject urlObj = QAndroidJniObject::fromString(ip);
    QAndroidJniObject usernameObj = QAndroidJniObject::fromString(usr);
    QAndroidJniObject passwordObj = QAndroidJniObject::fromString(pwd);
    QAndroidJniObject pathObj = QAndroidJniObject::fromString(path);

    QAndroidJniObject stringJni = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/ExtendsQtWithJava", "getFileList_SMB", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
                                                                            urlObj.object<jstring>(),
                                                                            pathObj.object<jstring>(),
                                                                            usernameObj.object<jstring>(),
                                                                            passwordObj.object<jstring>());
    QString result = stringJni.toString();
    if(result == "PERMISSION_DENIED"){
        return res;
    }
    res = result.split(",");
    return res;
}


bool JNIManager::uploadFile_SMB(QString ip, QString usr, QString pwd, QString remotePath, QString localPath){
    QAndroidJniObject urlObj = QAndroidJniObject::fromString(ip);
    QAndroidJniObject usernameObj = QAndroidJniObject::fromString(usr);
    QAndroidJniObject passwordObj = QAndroidJniObject::fromString(pwd);
    QAndroidJniObject remotePathObj = QAndroidJniObject::fromString(remotePath);
    QAndroidJniObject localPathObj = QAndroidJniObject::fromString(localPath);

    QAndroidJniObject stringJni = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/ExtendsQtWithJava", "uploadFile_SMB", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
                                                                            urlObj.object<jstring>(),
                                                                            remotePathObj.object<jstring>(),
                                                                            usernameObj.object<jstring>(),
                                                                            passwordObj.object<jstring>(),
                                                                            localPathObj.object<jstring>());

    QString result = stringJni.toString();
    if(result == "PERMISSION_DENIED"){
        return false;
    }
    return true;


}
bool JNIManager::downloadFile_SMB(QString ip, QString usr, QString pwd, QString remotePath, QString localPath){
    QAndroidJniObject urlObj = QAndroidJniObject::fromString(ip);
    QAndroidJniObject usernameObj = QAndroidJniObject::fromString(usr);
    QAndroidJniObject passwordObj = QAndroidJniObject::fromString(pwd);
    QAndroidJniObject remotePathObj = QAndroidJniObject::fromString(remotePath);
    QAndroidJniObject localPathObj = QAndroidJniObject::fromString(localPath);

    QAndroidJniObject stringJni = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/ExtendsQtWithJava", "downloadFile_SMB", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
                                                                            urlObj.object<jstring>(),
                                                                            remotePathObj.object<jstring>(),
                                                                            usernameObj.object<jstring>(),
                                                                            passwordObj.object<jstring>(),
                                                                            localPathObj.object<jstring>());
    QString result = stringJni.toString();
    if(result == "PERMISSION_DENIED"){
        return false;
    }
    return true;

}

void  JNIManager::uploadDBFile(){
    QString appPath = JNIManager::I().getAppPath();



    if(JNIManager::I().uploadFile_SMB("192.168.1.183", "宋阳", "songyang", "/微流控/9.工作报告/6.姜妮楠/tec.db", appPath+"/tec.db")){
        qDebug() << "上传成功";
    }else{
        qDebug() << "上传失败";
    }

}

QString JNIManager::getAppPath(){
    QAndroidJniObject stringJni = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/ExtendsQtWithJava", "getAppPath", "()Ljava/lang/String;");
    QString result = stringJni.toString();
    return result;
}
QStringList JNIManager::getUSBPath(){
    QAndroidJniObject stringJni = QAndroidJniObject::callStaticObjectMethod("org/qtproject/example/ExtendsQtWithJava", "getUSBPath", "()Ljava/lang/String;");
    QString result = stringJni.toString();

    QStringList res = result.split(",");

    for(int i = 0; i < res.size(); i ++){
        res[i] += "/";
    }

    res.removeLast();
    return res;
}

#endif
